package com.itcncp.wechat.app.rpc.service.impl;

import com.itcncp.common.annotation.BaseService;
import com.itcncp.common.base.BaseServiceImpl;
import com.itcncp.common.model.OrderCodeType;
import com.itcncp.common.model.XianxingModels;
import com.itcncp.common.model.XianxingResultModels;
import com.itcncp.common.util.DateUtils;
import com.itcncp.common.util.JsonUtils;
import com.itcncp.common.util.OrderUtils;
import com.itcncp.common.util.XianxingUtils;
import com.itcncp.wechat.app.dao.mapper.ItcsCityLimitInfoMapper;
import com.itcncp.wechat.app.dao.model.ItcsCityLimitInfo;
import com.itcncp.wechat.app.dao.model.ItcsCityLimitInfoExample;
import com.itcncp.wechat.app.rpc.api.ItcsCityLimitInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
* ItcsXianxingInfoService实现
* Created by itcncp on 2018/1/12.
*/
@Service
@Transactional
@BaseService
public class ItcsCityLimitInfoServiceImpl extends BaseServiceImpl<ItcsCityLimitInfoMapper, ItcsCityLimitInfo, ItcsCityLimitInfoExample>
        implements ItcsCityLimitInfoService {

    private static final Logger LOGGER = LoggerFactory.getLogger( ItcsCityLimitInfoServiceImpl.class);

    @Autowired
    ItcsCityLimitInfoMapper itcsCityLimitInfoDao;

    /**
     * 根据城市拼音查询城市限行信息
     * @param city_en
     * @return
     */

    @Override
    @Cacheable(value = "itcncp-ucenter-rpc-service-ehcache", key = "'getXianxingCity_' + #city_en")
    public ItcsCityLimitInfo getXianxingCity(String city_en) {

        Date xianDate = DateUtils.getDate("yyyy-MM-dd" );
        ItcsCityLimitInfoExample xianxingExample = new ItcsCityLimitInfoExample();
        xianxingExample.createCriteria().andCityEqualTo( city_en ).andCityDateEqualTo(   xianDate );
        List<ItcsCityLimitInfo> list =  itcsCityLimitInfoDao.selectByExample( xianxingExample );

        ItcsCityLimitInfo xianxing  =  new ItcsCityLimitInfo();
        if(null != list && list.size()>0){
            xianxing  = list.get( 0 );
            return  xianxing;
        }
        //没有查询到限行信息到请求API接口获取
        if(xianxing==null || xianxing.getCityLimitId()==null){

            XianxingModels xianxingModels= XianxingUtils.getXianxingCity(city_en);

            XianxingResultModels xianxingResult = xianxingModels.getResult();

            xianxing = new ItcsCityLimitInfo();
            xianxing.setCityLimitId( OrderUtils.createOrderId( OrderCodeType.CITY_LIMIT_ID ) );
            xianxing.setCity(xianxingResult.getCity());
            xianxing.setCityname(xianxingResult.getCityname());
            xianxing.setCityDate( xianDate );
            String decJson = JsonUtils.toJson(xianxingResult.getDes());
            xianxing.setLimitDec( decJson );
            xianxing.setFine(xianxingResult.getFine());
            xianxing.setHoliday(xianxingResult.getHoliday());
            Byte isXianxing= new Byte( xianxingResult.getIsxianxing() );

            if(isXianxing.intValue()!=1){
                isXianxing = new Byte( "2" );
            }
            xianxing.setIsxianxing( isXianxing );
            xianxing.setRemarks(xianxingResult.getRemarks());
            xianxing.setCityWeek( xianxingResult.getWeek() );
            String xxweihao = "";

            if(null != xianxingResult.getXxweihao()){
                for (int i = 0; i < xianxingResult.getXxweihao().length; i++) {
                    if(i==xianxingResult.getXxweihao().length-1){
                        xxweihao = xxweihao+xianxingResult.getXxweihao()[i].toString();
                    }else{
                        xxweihao = xxweihao+xianxingResult.getXxweihao()[i].toString()+",";
                    }
                }

                xianxing.setIsxianxing( new Byte( "1" ) );
            }

            xianxing.setXxweihao(xxweihao);

            itcsCityLimitInfoDao.insertSelective(xianxing);
        }

        /**
         *   "isxianxing": 1
         *   今日是否限行，1:是 0:否
         */
        if(xianxing.getIsxianxing()!=1){

            return null;

        }

        return xianxing;
    }

}